package com.wzx.data.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzx.basics.baseVo.PageVo;
import com.wzx.basics.baseVo.Result;
import com.wzx.basics.utils.PageInfo;
import com.wzx.basics.utils.PageUtil;
import com.wzx.basics.utils.ResultUtil;
import com.wzx.data.entity.Teacher;
import com.wzx.data.utils.NullUtils;
import com.wzx.data.entity.Course;
import com.wzx.data.service.ICourseService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wzx.data.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 课程 前端控制器
 * @author wzx
 * @since 2023/03/20
 */
@Slf4j
@RestController
@Api(tags = "课程管理接口")
@RequestMapping("/wzx/course")
@Transactional
public class CourseController {


    @Autowired
    private ICourseService courseService;

    @ApiOperation("添加课程基本信息")
    @PostMapping("save")
    public Result save(CourseVo vo) {
        courseService.saveCourseInfo(vo);
        return ResultUtil.success();
    }


    @ApiOperation("点播课程列表")
    @GetMapping("/list")
    public Result courseList(@ModelAttribute CourseVo vo, @ModelAttribute PageVo pageVo) {
        System.out.println(vo);
        final LambdaQueryWrapper<Course> lambda = new QueryWrapper<Course>().lambda();
        this.buildCondition(lambda,vo);
        final IPage<Course> page = courseService.page(new Page<>(pageVo.getPageNumber(), pageVo.getPageSize()), lambda);
        PageInfo<Course> pageInfo = new PageInfo<>(pageVo.getPageNumber(), pageVo.getPageSize());
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setTotalList(page.getRecords());
        return ResultUtil.data(pageInfo);
    }


    @ApiOperation("根据id获取课程信息")
    @GetMapping("get")
    public Result get(@RequestParam String id) {
        return ResultUtil.data(courseService.getCourseInfoById(id));
    }


    @ApiOperation("修改课程信息")
    @PostMapping("update")
    public Result update(CourseVo vo) {
        courseService.updateCourseId(vo);
        //课程id
        return ResultUtil.data(vo.getId());
    }


    @ApiOperation("id查询发布课程信息")
    @GetMapping("getCoursePublishVo/{id}")
    public Result getCoursePublishVo(@PathVariable String id) {
        CourseVo vo = courseService.getCoursePublishVo(id);
        return ResultUtil.data(vo);
    }

    @ApiOperation("课程最终发布")
    @PutMapping("publishCourse/{id}")
    public Result publishCourse(@PathVariable String id) {
        courseService.publishCourse(id);
        return ResultUtil.success();
    }

    @ApiOperation("删除课程")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable String id) {
        courseService.removeCourseId(id);
        return ResultUtil.success();
    }

    @ApiOperation("查询所有课程")
    @GetMapping("findAll")
    public Result findAll() {
        List<Course> list = courseService.findlist();
        return ResultUtil.success();
    }
    @ApiOperation("根据教师Id查询课程")
    @GetMapping("/getByTeacher")
    public Result getByTeacher(@RequestParam String teacherId) {
        LambdaQueryWrapper<Course> lambda = new LambdaQueryWrapper<Course>();
        lambda.eq(Course::getTeacherId,teacherId);
        List<Course> list = courseService.list(lambda);
        return ResultUtil.data(list);
    }
    @ApiOperation(value = "获取分页列表")
    @GetMapping("/page")
    public Result index( @ModelAttribute CourseVo vo, @ModelAttribute PageVo pageVo) {
        Page<Course> pageParam = new Page<>(pageVo.getPageNumber(), pageVo.getPageSize());
        Map<String,Object> map = courseService.findPageCourse(pageParam, vo);
        return ResultUtil.data(map);
    }

    /**
     * 构建查询条件
     * @param lambda
     * @param vo
     */
    private void buildCondition(LambdaQueryWrapper<Course> lambda, CourseVo vo) {
        if (ObjectUtils.isNotEmpty(vo.getTeacherId())){
            lambda.eq(Course::getTeacherId, vo.getTeacherId());
        }
        //根据startTime
        if (ObjectUtils.isNotEmpty(vo.getStartTime())){
            lambda.ge(Course::getCreateTime, vo.getStartTime());
        }
        //根据endTime
        if (ObjectUtils.isNotEmpty(vo.getEndTime())){
            lambda.le(Course::getCreateTime, vo.getEndTime());
        }
        lambda.eq(Course::getDelFlag,0);
        lambda.orderByDesc(Course::getCreateTime);
    }
}
